package cn.fu.annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 
 * 
 * 
 * @author 99fu
 *
 */

public class ParseAnnotation {

	public static void main(String[] args) {
		
		try {
			//1.使用类加载器加载类
			Class c = Class.forName("cn.fu.annotation.Child");
			//2.找到类上面的注解
			boolean isExist = c.isAnnotationPresent(Description.class);
			if(isExist){
				//3.拿到注解实例
				Description annotation = (Description)c.getAnnotation(Description.class);
				System.err.println("age:"+annotation.age() +";name="+annotation.name()+";gender:"+annotation.gender());
			}
			//-----------方法上注解---------------------------------
			//4.找到方法上的注解
			Method[] methods = c.getMethods();
			for (Method method : methods) {
				boolean isMethodExist = method.isAnnotationPresent(Description.class);
				if(isMethodExist){
					Description annotation = method.getAnnotation(Description.class);
					System.err.println("age:"+annotation.age() +";name="+annotation.name()+";gender:"+annotation.gender());
				}
			}
			//------------另一种解析方法----------------------------------
			System.err.println("----另一种解析方法------");
			for(Method m:methods){
				Annotation[] annotations = m.getAnnotations();
				for (Annotation annotat : annotations) {
					if(annotat instanceof Description){
						Description annotation=(Description)annotat;
						System.err.println("age:"+annotation.age() +";name="+annotation.name()+";gender:"+annotation.gender());
					}
				}
			}
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
